package cn.findviewbydream.evil.main.service.impl;

import cn.findviewbydream.evil.main.dao.SettingMealDAO;
import cn.findviewbydream.evil.main.entity.BaseMealTypeEntity;
import cn.findviewbydream.evil.main.entity.BaseTypeOfWorkEntity;
import cn.findviewbydream.evil.main.entity.MealEntity;
import cn.findviewbydream.evil.main.exception.SettingMealException;
import cn.findviewbydream.evil.main.exception.UploadImgException;
import cn.findviewbydream.evil.main.service.SettingMealService;
import cn.findviewbydream.evil.main.utils.FileWrite;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.List;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.main.service.impl
 * 时间：17-2-23
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
@Service
public class SettingMealServiceImpl implements SettingMealService
{
    private final SettingMealDAO dao;

    @Autowired
    public SettingMealServiceImpl(SettingMealDAO settingMealDAO) {this.dao = settingMealDAO;}


    @Override
    public List<BaseMealTypeEntity> getBaseMealTypeList()
    {
        return dao.selectBaseMealTypeList();
    }

    @Override
    public boolean postNewBaseMealType(String type)
    {
        BaseMealTypeEntity entity = new BaseMealTypeEntity();
        entity.setType(type);
        entity.setState(true);
        return dao.insertBaseMealType(entity) == 1;
    }

    @Override
    public boolean postBaseMealTypeState(int id, boolean state)
    {
        BaseMealTypeEntity entity = new BaseMealTypeEntity();
        entity.setMealTypeId(id);
        entity.setState(state);
        return dao.updateBaseMealTypeState(entity) == 1;
    }

    @Override
    public boolean postBaseMealType(int id, String type)
    {
        BaseMealTypeEntity entity = new BaseMealTypeEntity();
        entity.setMealTypeId(id);
        entity.setType(type);
        return dao.updateBaseMealType(entity) == 1;
    }

    @Override
    public List<MealEntity> getMealList(Integer typeId)
    {
        if (typeId != null)
        {
            BaseMealTypeEntity entity = new BaseMealTypeEntity();
            entity.setMealTypeId(typeId);
            return dao.selectMealList(entity);
        }
        else
            return dao.selectMealList(null);
    }

    @Override
    public boolean postMealMarket(int mealId, boolean market)
    {
        return dao.updateMealMarket(mealId, market) == 1;
    }

    @Override
    public MealEntity getMeal(int mealId)
    {
        return dao.selectMealById(mealId);
    }

    @Override
    public List<BaseMealTypeEntity> getMealTypeList()
    {
        return dao.selectBaseMealTypeList();
    }

    @Override
    public List<BaseTypeOfWorkEntity> getCookTypeList()
    {
        return dao.selectBaseTypeOfWorkList();
    }

    @Override
    @Transactional
    public Boolean postNewMeal(String name, String synopsis, Integer mealTypeId, Integer typeOfWorkId,
                               Float price, Float cost, Integer mealMonthStart, Integer mealMonthEnd,
                               CommonsMultipartFile img) throws Exception
    {
        MealEntity entity = new MealEntity();
        entity.setName(name);
        entity.setSynopsis(synopsis != null && !synopsis.equals("") ? synopsis : null);
        entity.setMealType(new BaseMealTypeEntity(mealTypeId));
        entity.setCookType(new BaseTypeOfWorkEntity(typeOfWorkId));
        entity.setPrice((int) (price * 100));
        entity.setCost(cost != null ? (int) (cost * 100) : null);
        entity.setMarket(true);

        if (mealMonthStart != null && mealMonthEnd != null)
            entity.setMonthToMarket(mealMonthStart + "-" + mealMonthEnd);
        else
            entity.setMonthToMarket(null);

        try
        {
            dao.insertMeal(entity);
        }
        catch (Exception e)
        {
            throw new SettingMealException("新增菜品错误！", e);
        }

        if (img != null)
        {
            String filePath = System.getProperty("root.path") + "static/upload/img/meal";

            try
            {
                String imgName = img.getOriginalFilename();
                imgName = imgName.replace(imgName.substring(0, imgName.lastIndexOf(".")), entity.getMealId() + "");
                FileWrite.write(img, filePath, imgName);
                entity.setImgs(new Gson().toJson(new String[]{imgName}));
            }
            catch (IOException e)
            {
                throw new UploadImgException("图片上传失败！", e);
            }
        }

        try
        {
            return dao.updateMeal(entity) == 1;
        }
        catch (Exception e)
        {
            throw new SettingMealException("图片信息插入错误！", e);
        }
    }

    @Override
    public Boolean postMeal(int id, String name, String synopsis, Integer mealTypeId, Integer typeOfWorkId,
                            Float price, Float cost, Integer mealMonthStart, Integer mealMonthEnd,
                            CommonsMultipartFile img) throws Exception
    {
        MealEntity entity = dao.selectMealById(id);
        entity.setMealId(id);
        entity.setName(name);
        entity.setSynopsis(synopsis != null && !synopsis.equals("") ? synopsis : null);
        entity.setMealType(new BaseMealTypeEntity(mealTypeId));
        entity.setCookType(new BaseTypeOfWorkEntity(typeOfWorkId));
        entity.setPrice((int) (price * 100));
        entity.setCost(cost != null ? (int) (cost * 100) : null);
        if (mealMonthStart != null && mealMonthEnd != null)
            entity.setMonthToMarket(mealMonthStart + "-" + mealMonthEnd);
        else
            entity.setMonthToMarket(null);

        if (img != null)
        {
            String filePath = System.getProperty("root.path") + "static/upload/img/meal";

            try
            {
                String imgName = img.getOriginalFilename();
                imgName = imgName.replace(imgName.substring(0, imgName.lastIndexOf(".")), id + "");
                FileWrite.write(img, filePath, imgName);
                entity.setImgs(new Gson().toJson(new String[]{imgName}));
            }
            catch (IOException e)
            {
                throw new UploadImgException("图片上传失败！", e);
            }
        }

        try
        {
            return dao.updateMeal(entity) == 1;
        }
        catch (Exception e)
        {
            throw new SettingMealException("菜品信息更新错误！", e);
        }
    }
}
